package karaoke.controller;

import java.util.List;

import karaoke.excepcion.DAOExcepcion;
import karaoke.json.CustomGenericResponse;
import karaoke.json.CustomReservaResponse;
import karaoke.modelo.Reserva;
import karaoke.negocio.GestionReserva;
import karaoke.negocio.GestionSala;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("reserva")
public class ReservaController {

	@RequestMapping(method = RequestMethod.GET)
    public String obtenerPaginaReserva() {
    	return "/mantenimiento/reserva";
	}
	
	
	@RequestMapping(value="/lista", method = RequestMethod.GET)
    public @ResponseBody CustomReservaResponse listaReserva(){
		CustomReservaResponse res= new CustomReservaResponse();
		 GestionReserva gestion = new GestionReserva();
		 
		 try {
			List<Reserva> lstReserva= gestion.buscarPorNombre("");
			res.setRows(lstReserva);
			res.setRecords( String.valueOf(lstReserva.size()) );
			res.setPage( "1" );
        	
        	// Same. Assign a dummy total pages
			res.setTotal( "10" );
        	
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return res; 
	 }
	
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse add(Reserva reserva) {
    	
		GestionReserva gestion = new GestionReserva();
		 try {
			 System.out.println("cliente : " + reserva.getCliente().getIdCliente());
			 reserva = gestion.insertar(reserva.getFecha(),reserva.getEstado(),reserva.getDescripcion(),reserva.getCliente().getIdCliente(),reserva.getSala().getIdsala());
    	
    	// Check if successful
    	if ( reserva.getIdreserva()>0 ) {
    		// Success. Return a custom response
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
    	} else {
    		// A failure. Return a custom response as well
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
    	}
		 } catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				CustomGenericResponse response = new CustomGenericResponse();
				response.setSuccess(false);
				response.setMessage("Action failure!");
	    		return response;
	    		// :(
				
			}
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse edit(
    		@RequestParam("id") String id,
    		Reserva reserva) {
    	
		GestionReserva gestion = new GestionReserva();
		 try {
			
			 reserva.setIdreserva(Integer.valueOf(id));
			 reserva=gestion.actualizar(reserva.getIdreserva(),reserva.getFecha(),reserva.getEstado(),reserva.getDescripcion(),reserva.getCliente().getIdCliente(),reserva.getSala().getIdsala());
    	
    	
    	// Check if successful
    	if ( reserva.getIdreserva()!=0 ) {
    		// Success. Return a custom response
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
    	} else {
    		// A failure. Return a custom response as well
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
    	}
		 } catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse delete(
    		@RequestParam("id") String id
    ) {
    	
    	
    	GestionReserva gestion= new GestionReserva();
    	try{
    		gestion.eliminar(Integer.valueOf(id));
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
	 } catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
		}
    		
    	
	}
	
	
}
